package com.wanxi.dao.impl;

import com.wanxi.dao.ProductDao;
import com.wanxi.model.ProductModel;
import com.wanxi.model.UserModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    public int add(ProductModel model) {
        String sql = "INSERT INTO `product` (" +
                "`name`, `normal_price`, `price`, " +
                "`content`, `is_show`, `is_recommend`, " +
                "`img_href`, `create_time`, `update_time`," +
                " `remark`, `enable`, `where_show`, " +
                "`cuisine_id`, `style`) VALUES (" +
                "'" + model.getName() + "', '" + model.getNormalPrice() + "', '" + model.getMarketPrice() + "', " +
                "'" + model.getContent() + "', '" + model.getIsShow() + "', '" + model.getIsRecommend() + "', " +
                "'" + model.getImgHref() + "', now(), now(), " +
                "'', '1', '3', " +
                "'" + model.getCuisineId() + "', '1');";
        return JDBC.update(sql);
    }

    public int update(ProductModel model) {
        String sql = "";
        return JDBC.update(sql);
    }

    public int delete(ProductModel model) {
        String sql = "";
        return JDBC.update(sql);
    }

    public ProductModel findById(ProductModel model) {
        String sql = "select * from product where id=" + model.getId();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return ProductModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .normalPrice(query.getBigDecimal("normal_price"))
                        .marketPrice(query.getBigDecimal("price"))
                        .content(query.getString("content"))
                        .isShow(query.getInt("is_show"))
                        .isRecommend(query.getInt("is_recommend"))
                        .imgHref(query.getString("img_href"))
                        .whereShow(query.getInt("where_show"))
                        .cuisineId(query.getInt("cuisine_id"))
                        .build();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    public List<ProductModel> findAll(ProductModel model) {
        String sql = "select * from ( " +
                "SELECT p.*,c.name cuisineName FROM product p " +
                "left join cuisine c " +
                "on p.cuisine_id=c.id) " +
                "temp  " +
                "where true ";
        sql += getWhereSql(model);
        sql += " order by id desc ";
        sql += " limit " + (model.getPage() - 1) * model.getLimit() + "," + model.getLimit() + "";
        ResultSet query = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (query.next()) {
                list.add(ProductModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .normalPrice(query.getBigDecimal("normal_price"))
                        .marketPrice(query.getBigDecimal("price"))
                        .content(query.getString("content"))
                        .isShow(query.getInt("is_show"))
                        .isRecommend(query.getInt("is_recommend"))
                        .imgHref(query.getString("img_href"))
                        .whereShow(query.getInt("where_show"))
                        .cuisineId(query.getInt("cuisine_id"))
                        .cuisineName(query.getString("cuisineName"))
                        .build());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    private String getWhereSql(ProductModel productModel) {
        String sql = "";
        if (productModel.getName() != null || !"".equals(productModel.getName())) {
            sql += " and name like '%" + productModel.getName() + "%'";
        }
        return sql;
    }

    @Override
    public Integer count(ProductModel productModel) {
        String sql = "select count(*) count from ( " +
                "SELECT p.*,c.name cusineName FROM product p " +
                "left join cuisine c " +
                "on p.cuisine_id=c.id) " +
                "temp  " +
                "where true ";
        sql += getWhereSql(productModel);
        return JDBC.count(sql);
    }


}
